const app = getApp();
const currentTime = new Date().toLocaleDateString();//当前日期
Component({
  properties: {
    shopId: {
      type: Number,
      value: 0,
    },
  },
  methods: {
    getAds() {
      let module = app.siteInfo.module; // by lili, 2019/03/22  --- 兼容商城多门店版
      let params = {};
      if (module === 'ws_micro_shop') {
        params = {
          shop_id: app.ws_micro_shop_globalData.shopid,
        };
      }

      //在缓存中查找是否有商店shopid
      //每个商店在活动周期内只打开一次或者缓存丢失
      let shop_id_list = wx.getStorageSync('shop_id_list') || [];

      const idList = shop_id_list.filter(v => v === this.data.shopId);
      if (idList.length > 0) return//如果内存中已存则不再请求弹窗
      
      // const adsTime = wx.getStorageSync('adsTime') || 0;
      
      //每24小时执行一次或者缓存丢失
      // const currentTime = new Date().getTime();//当前时间
      // if (currentTime - adsTime < 86400000) return

      // 每天第一次打开执行一次或者缓存丢失
      // if (currentTime === adsTime) return;
      
      app.util.request({
        url: 'wxapp/advert/advertlist',
        data: params,
        success: (res) => {
          this.setData({
            adList: res.data.data,
            showAds: res.data.data && res.data.data.length > 0,
          });
          if (idList.length === 0 && this.data.showAds) {//如果成功弹窗则保存shop_id
            shop_id_list.push(this.data.shopId);
            wx.setStorageSync('shop_id_list', shop_id_list);
          }
        },
      });
    },
    hideAds() {
      this.setData({
        showAds: false,
      });
      // wx.setStorageSync('adsTime', currentTime);
    },
    goNavigate(e) {
      const {
        type = '',
          path = '',
          appid = '',
          module = '',
      } = e.currentTarget.dataset.item;
      let params = {}; // by lili, 2019/03/22  --- 兼容商城多门店版
      if (module === 'ws_micro_shop') {
        params = {
          shop_id: app.ws_micro_shop_globalData.shopid,
        };
      }
      if (type === '1') {
        app.util.navigateTo(`${path}`,params);
      } else if (type === '2') {
        wx.navigateToMiniProgram({
          appId: appid,
          path,
          envVersion: 'release',
        });
      } else if (type === '3') {
        const url = `/${module}/pages/web/web?webview_url=${path}`;
        wx.navigateTo({
          url,
        });
      }
    },
  },
  // attached() {
  //   this.getAds();
  // },
  ready: function () {
    this.getAds();
  },
});